import binascii
import pandas as pd
import numpy as np
import sys


def big_small_endian_convert(big_endian_str):
    return int(binascii.hexlify(binascii.unhexlify(big_endian_str)[::-1]), base = 16)

def load_data(filename):
    df_data = pd.read_csv(filename)
    # df_data = df_data[df_data['data'].notnull()]
    # print(df_data)
    df_data = df_data['data'].map(lambda x: big_small_endian_convert(x[0:8]))

    return df_data


def get_tcpdump_loss(file_sender, file_receiver):
    sender_data = load_data(file_sender)
    receiver_data = load_data(file_receiver)

    sender = set(sender_data)
    receiver = set(receiver_data)

    diff  = sender - receiver
    # print(sorted(diff))
    print('sender NIC list num', len(sender_data))
    print('receiver NIC list num', len(receiver_data))
    print('sender NIC set num', len(sender))
    print('receiver NIC set num', len(receiver))
    print('packet loss num', len(diff))
    print()
    return diff



if __name__ == "__main__":
    speed = sys.argv[1]
    buffer = sys.argv[2]

    # file1 = f'compressed_vm_enp0s5_{speed}_{buffer}.pcap.csv'
    # file2 = f'compressed_laptop_en0_{speed}_{buffer}.pcap.csv'
    # file3 = f'compressed_hombre_enp4s0_{speed}_{buffer}.pcap.csv'


    file1 = f'vm_enp0s5_{speed}_{buffer}.pcap.csv'
    file2 = f'laptop_en0_{speed}_{buffer}.pcap.csv'
    file3 = f'hombre_enp4s0_{speed}_{buffer}.pcap.csv'

    loss_set = get_tcpdump_loss(file1, file2)
    loss_set = get_tcpdump_loss(file1, file3)
    loss_set = get_tcpdump_loss(file2, file3)
    
    # loss_set = sorted(loss_set)
    # print(loss_set)
    # with open('wirelessloss.csv', 'w') as f:
    #     for seq in loss_set:
    #         f.write(str(seq) + '\n')